package com.changanw.common.feign;

import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import io.seata.rm.tcc.api.LocalTCC;
import io.seata.rm.tcc.api.TwoPhaseBusinessAction;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@LocalTCC //表示使用TCC模式管理事务
public interface StorageServiceApi {
    /**
     * 这是一阶段的操作
     * 用来检查资源，检查账户是否存在，余额是否充足。充足的话就冻结起来
     * 这个方法由程序员手动调用，
     * @return
     * //@TwoPhaseBusinessAction  两阶段提交的注解，指定提交和回滚方法
     */
    @TwoPhaseBusinessAction(name = "storageServiceApi",commitMethod = "commit",rollbackMethod = "rollback")
    @RequestMapping("/storage/deduct/prepare")
    boolean prepare(@RequestBody BusinessActionContext context,
                    @RequestParam("productId") @BusinessActionContextParameter(paramName = "productId") String productId,
                    @RequestParam("count") @BusinessActionContextParameter(paramName = "count") Integer count);

    /**
     * 真正的提交操作，主要是扣款操作，（操作冻结的资源即可）
     * 自动调用
     * @return
     */
    @RequestMapping("/storage/deduct/commit")
    boolean commit(@RequestBody BusinessActionContext context);

    /**
     * 这是二阶段的回滚操作
     * 自动调用
     * @return
     */
    @RequestMapping("/storage/deduct/rollback")
    boolean rollback(@RequestBody BusinessActionContext context);
}
